package com.assessment.servermanager.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.assessment.servermanager.commen.utils.StringUtils;
import com.assessment.servermanager.entity.TbQuestion;
import com.assessment.servermanager.mapper.TbQuestionMapper;
import com.assessment.servermanager.service.ITbQuestionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author jiajia
 * @date 2023-08-28
 */
@Service
public class TbQuestionServiceImpl implements ITbQuestionService {
    @Autowired
    private TbQuestionMapper tbQuestionMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public TbQuestion selectTbQuestionById(Long id) {
        return tbQuestionMapper.selectById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param tbQuestion 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<TbQuestion> selectTbQuestionList(TbQuestion tbQuestion) {
        LambdaQueryWrapper<TbQuestion> queryWrapper = new LambdaQueryWrapper<TbQuestion>()
                .eq(tbQuestion.getId() != null,TbQuestion::getId,tbQuestion.getId())
                .eq(StringUtils.isNotEmpty(tbQuestion.getContent()),TbQuestion::getId,tbQuestion.getId())
                .eq(StringUtils.isNotEmpty(tbQuestion.getChooseA()),TbQuestion::getChooseA,tbQuestion.getChooseA())
                .eq(StringUtils.isNotEmpty(tbQuestion.getChooseB()),TbQuestion::getChooseB,tbQuestion.getChooseB())
                .eq(StringUtils.isNotEmpty(tbQuestion.getChooseC()),TbQuestion::getChooseC,tbQuestion.getChooseC())
                .eq(StringUtils.isNotEmpty(tbQuestion.getChooseD()),TbQuestion::getChooseD,tbQuestion.getChooseD())
                .eq(StringUtils.isNotEmpty(tbQuestion.getAnswer()),TbQuestion::getAnswer,tbQuestion.getAnswer());
        return tbQuestionMapper.selectList(queryWrapper);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param tbQuestion 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertTbQuestion(TbQuestion tbQuestion) {
        return tbQuestionMapper.insert(tbQuestion);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param tbQuestion 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateTbQuestion(TbQuestion tbQuestion) {
        return tbQuestionMapper.updateById(tbQuestion);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTbQuestionByIds(Long[] ids) {
        return tbQuestionMapper.deleteBatchIds(Arrays.stream(ids).collect(Collectors.toList()));
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteTbQuestionById(Long id) {
        return tbQuestionMapper.deleteById(id);
    }

    /**
     * 根据数量生成题目
     * @param questionNum
     * @return
     */
    @Override
    public List<TbQuestion> generterQuestion(Integer questionNum) {
        QueryWrapper<TbQuestion> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("RAND()").last("limit "+questionNum);
        return tbQuestionMapper.selectList(queryWrapper);
    }

    @Override
    public List<TbQuestion> selectQuestionByPaperId(Long paperId) {
        return tbQuestionMapper.selectQuestionByPaperId(paperId);
    }
}
